/*
*  Copyright 2019-2020 Sun Feng
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.ruibo.modules.weixin.rest;

import com.ruibo.annotation.AnonymousAccess;
import com.ruibo.aop.log.Log;
import com.ruibo.modules.weixin.domain.DustReport;
import com.ruibo.modules.weixin.domain.RegionCode;
import com.ruibo.modules.weixin.domain.ReportAttachment;
import com.ruibo.modules.weixin.service.ReportAttachmentService;
import com.ruibo.modules.weixin.service.dto.RegionCodeDto;
import com.ruibo.modules.weixin.service.dto.ReportAttachmentDto;
import com.ruibo.modules.weixin.service.dto.ReportAttachmentQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author 孙峰
* @date 2020-11-06
**/
@RestController
@RequiredArgsConstructor
@RequestMapping("/weixin/reportAttachment")
public class ReportAttachmentController {

    private final ReportAttachmentService reportAttachmentService;



    @Log("导出数据")
    @GetMapping(value = "/download")
    public void download(HttpServletResponse response, ReportAttachmentQueryCriteria criteria) throws IOException {
        reportAttachmentService.download(reportAttachmentService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询ReportAttachment")
    public ResponseEntity<Object> query(ReportAttachmentQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(reportAttachmentService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增ReportAttachment")
    public ResponseEntity<Object> create(@Validated @RequestBody ReportAttachment resources){
        return new ResponseEntity<>(reportAttachmentService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改ReportAttachment")
    public ResponseEntity<Object> update(@Validated @RequestBody ReportAttachment resources){
        reportAttachmentService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除ReportAttachment")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        reportAttachmentService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @AnonymousAccess
    @GetMapping("/dustReportId/{dustReportId}")
    public ResponseEntity<Object> findByDustReportId(@PathVariable("dustReportId") Long dustReportId) {
        List<ReportAttachmentDto> reportAttachmentList = reportAttachmentService.findByDustReportId(dustReportId);
        return new ResponseEntity<>(reportAttachmentList, HttpStatus.OK);
    }


}
